/*
 * LookupTable.c
 *
 *  Created on: Oct 2, 2008
 *      Author: V
 */

#include "LookupTable.h"
#include <assert.h>

pLookupTable table = NULL;

pLookupTable LookupTable_new()
{
	pLookupTable ret = (pLookupTable) GC_malloc(sizeof(LookupTable));
	ret->labels = (StringList) GC_malloc(10*sizeof(pString));
	ret->anonymousStatements =
		(AnonymousStatementList) GC_malloc(10*sizeof(pAnonymousStatement));

	ret->labels[0] = NULL; // NULL terminated
	ret->anonymousStatements[0] = NULL;
	ret->currentSize = 0;
	ret->maxSize = 10;
	ret->add = &LookupTable_add;
	ret->getMain = &LookupTable_getMain;
	assert(ret != NULL);
	return ret;
}

int LookupTableInit()
{
	if (table != NULL) return 1; // nothing to do
	table = LookupTable_new();
	return (table != NULL);
}

int LookupTable_add(
		pLookupTable tableObj,
		pString label,
		pAnonymousStatement statement)
{
	if (tableObj == NULL) tableObj = LookupTable_new();
	assert(tableObj != NULL);
	assert(label != NULL);
	assert(statement != NULL);

	if (tableObj->currentSize == tableObj->maxSize) ; // resize (not impl)

	// copy reference to label in first avail spot
	tableObj->labels[tableObj->currentSize] = label;
	tableObj->anonymousStatements[tableObj->currentSize] = statement;
	tableObj->currentSize++;
	return (tableObj != NULL);
}

pAnonymousStatement LookupTable_getMain(pLookupTable table)
{
	return table->anonymousStatements[0];
}
